<?php

if (isset($_GET['act'])) $act = $_GET['act'];

$list_url = "index.php?mod=news&act=list";
$delete_url = $list_url . "&task=delete&id=";
$add_url = "index.php?mod=news&act=add";
$edit_url = "index.php?mod=news&act=edit&id=";
?>

<div id="action-menu">
    <ul>
        <li><a class="list-action" href="<?php echo $list_url ?>">Quản lý bài viết</a></li>
        <li><a class="add-action" href="<?php echo $add_url ?>">Thêm bài viết</a></li>
    </ul>
</div>

<div id="main-content">
<?php

if ($act == "list") {
?>
    <h3 class='action-title'>Danh sách các bài viết </h3>
    <?php
    if (isset($_GET['task']) && $_GET['task'] == 'delete') {
        if (isset($_GET['id'])) $id = $_GET['id'];
        if (isset($_GET['title'])) $title = $_GET['title'];
        $sql = "delete from news where id = $id";
        if ($db->query($sql)) notice("Đả xóa bài viết: <strong>$title</strong>.");
        else notice("Lổi: chưa xóa được bài viết.");
    }
    ?>
    <div id="list-posts">
        <div class="header-list">
            <label class="header-title">Tên bài viết</label>
            <label class="header-viewed">Lượt đọc</label>
            <label class="header-edit">Sửa</label>
            <label class="header-delete">Xóa</label>
            <div class="clear"></div>
        </div>
        <?php
            $page = (isset($_GET['page'])) ? $_GET['page'] : 1;
            $limit = $config['news_per_page'];
            $start = $start = $limit * ($page - 1);
            $sql = "select * from news order by id desc limit $start, $limit ";
            $data = $db->get_data($db->query($sql));
            
            if (!empty($data)) :
            foreach ($data as $post) :
                $edit_post = $edit_url.$post['id'];
                $delete_post = $delete_url.$post['id']."&title=".$post['title'];
        ?>
        <div class="row-post">
            <label class="header-title"><a href="<?php echo $edit_post ?>"><?php echo $post['title'] ?></a></label>
            <label class="header-viewed"><?php echo $post['viewed'] ?></label>
            <label class="header-edit"><a href="<?php echo $edit_post ?>" title="Sửa <?php echo $post['title'] ?>" class="edit-post">Sửa</a></label>
            <label class="header-delete"><a href="<?php echo $delete_post ?>" title="Xóa <?php echo $post['title'] ?>"  class="delete-post">Xóa</a></label>
            <div class="clear"></div>
        </div>
        <?php
            endforeach;
            endif;
            ViewPages($config['news_per_page'], "news", "news");
        ?>
    </div>
    


<?php
} else if ($act == "add") {
    echo "<h3 class='action-title'>Thêm bài viết mới</h3>";
    
    if (isset($_POST['submit'])) {
        $title = urldecode(stripslashes($_POST['title']));
        //$cat_id = $_POST['cat_id'];
        $thumbnail = urldecode(stripslashes($_POST['thumbnail']));
        $summary = urldecode(stripslashes($_POST['summary']));
        $content = urldecode(stripslashes($_POST['content']));
        $created = date('Y-m-d h:i:s');
        $modified = date('Y-m-d h:i:s');
        $user_id = 1;
        $sql = "insert into news (title,thumbnail,summary,content,created,modified,user_id) values 
                            ('{$title}','{$thumbnail}','{$summary}','{$content}','{$created}','{$modified}',{$user_id})";
        if ($db->query($sql)) notice("Đã cập nhật bài viết.");
        else notice("Lỗi: chưa cập nhật được.");
    }
?>

    <form id="add-post" method="post" action="">
        <label class="post-label">Tiêu đề:</label>
        <input name="title" type="text" size="100" class="required" minlength="10" />
        <label class="post-label">Chuyên mục:</label>
        <label class="post-label">Thumbnail:</label>
        <input name="thumbnail" type="text" size="100"  />
        <label class="post-label">Tóm tắt:</label>
        <textarea name="summary" cols="100" rows="10" class="required"></textarea>
        <label class="post-label">Nội dung:</label>
        <textarea name="content" cols="100" rows="20" class="required"></textarea>
        <input type="submit" name="submit" class="button" value="Cập nhật" />
        <input type="reset" name="reset" class="button" value="Hủy" />
    </form>
	
	<script>
	$("#add-post").validate();
	</script>

<?php
} else if ($act == "edit") {
    echo "<h3 class='action-title'>Sửa bài viết</h3>";
    
    $id = (isset($_GET['id'])) ? $_GET['id'] : 0;
    
    if (isset($_POST['submit'])) {
        $title = urldecode(stripslashes($_POST['title']));
        $thumbnail = urldecode(stripslashes($_POST['thumbnail']));
        $summary = urldecode(stripslashes($_POST['summary']));
        $content = urldecode(stripslashes($_POST['content']));
        $modified = date('Y-m-d h:i:s');
        $user_id = 1;                   
        $sql =  "update news set title='$title', thumbnail='$thumbnail', summary='$summary', content='$content', modified='$modified' where id = $id";

        if ($db->query($sql)) notice("Đã cập nhật bài viết.");
        else notice("Lỗi: chưa cập nhật được.");
    }
    
    
    $sql = "select * from news where id = $id";
    $data = $db->fetch_array($db->query($sql));
    if (!empty($data)) :
?>

    <form id="add-post" method="post" action="">
        <label class="post-label">Tiêu đề:</label>
        <input name="title" type="text" size="100" class="required" minlength="10" value='<?php echo $data['title'] ?>' />
        <label class="post-label">Chuyên mục:</label>
        <label class="post-label">Thumbnail:</label>
        <input name="thumbnail" type="text" size="100" value='<?php echo $data['thumbnail'] ?>' />
        <label class="post-label">Tóm tắt:</label>
        <textarea name="summary" cols="100" rows="10" class="required"><?php echo $data['summary'] ?></textarea>
        <label class="post-label">Nội dung:</label>
        <textarea name="content" cols="100" rows="20" class="required"><?php echo $data['content'] ?></textarea>
        <input type="submit" name="submit" class="button" value="Cập nhật" />
        <input type="reset" name="reset" class="button" value="Hủy" />
    </form>
	<script>
	$("#add-post").validate();
	</script>
<?php
    endif;
}
?>
</div>